import {toast,loading} from './toast.js'
export default{
	async detectionLogin(_this){//检测登录
		let token = uni.getStorageSync('user_token');
		console.log(token)
		let self = this;
		if(!token){//未检测到token直接去登陆
			uni.navigateTo({
				url:'/pages/signIn/login'
			})
			return false;
		}
		let res  = await _this.$http('post','getUserInfo')
		console.log(JSON.stringify(res))
		if (res.code == 401&&res.msg == '请登录后操作') {
			// uni.clearStorage();//清除本地缓存
			// uni.clearStorageSync();//清除本地缓存
			uni.removeStorage({
				key:'user_info'
			});
			uni.removeStorageSync('token');
			uni.navigateTo({
				url:'/pages/login/login'
			})
		}else if(res.code == 1){
			uni.getStorage({
				key:'user_info',
				success(res) {
					console.log(JSON.stringify(res))
					uni.setStorageSync('myUsername',res.data.user_name);
					if(res.data.is_store == 2){
						uni.switchTab({
							url:'/pages/home/home'
						});
					}
				}
			})
		}
	},
	//时间转换
	timestampToTime(timestamp) {
		var date = new Date(timestamp * 1000);//时间戳为10位需*1000，时间戳为13位的话不需乘1000
		var Y = date.getFullYear() + '-';
		var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
		var D = date.getDate() + ' ';
		var h = date.getHours()<10?'0'+date.getHours()+':':date.getHours() + ':';
		var m = date.getMinutes()<10?'0'+date.getMinutes()+':':date.getMinutes() + ':';
		var s = date.getSeconds()<10?'0'+date.getSeconds():date.getSeconds() ;
		return Y + M + D+ h+m+s;
		//return h+m;
	},
	
	//选择时间处理
	getDate(type) {
		const date = new Date();
		let year = date.getFullYear();
		let month = date.getMonth() + 1;
		let day = date.getDate();

		if (type === 'start') {
			year = year - 60;
		} else if (type === 'end') {
			year = year + 2;
		}
		month = month > 9 ? month : '0' + month;;
		day = day > 9 ? day : '0' + day;
		return `${year}-${month}-${day}`;
	},
	//获取设备信息
	getSystemInfo(_this){
		uni.getSystemInfo({
		    success: function (res) {
				console.log(JSON.stringify(res.platform))
		    // _this.$store.commit('platform',res.platform);
		    }
		})
	},
	// #ifdef MP-WEIXIN
	//经纬度改省市区
	async getDistrict(latitude, longitude,_this) {
		let keys = 'SGXBZ-6X3K6-NYLSF-MALZD-QC6PK-BABOS';
		wx.request({
		  url: `https://apis.map.qq.com/ws/geocoder/v1/?location=${latitude},${longitude}&key=${keys}`,
		  header: {
			'Content-Type': 'application/json'
		  },
		  success: function(res) {
			console.log(res.data.result.address_component.district, res.data.result)
			 
			// 省
			let province = res.data.result.address_component.province;
			// 市
			let city = res.data.result.address_component.city;
			// 区
			let district = res.data.result.address_component.district;
		 
			console.log(province,city,district)
			//_this.$store.commit('actives',city);
		  }
		})
	  },
	// #endif
}